
import java.io.Serializable;
import java.util.Random;

public class Substitution extends Message implements Serializable {

    private int subKey;

    public Substitution(User u, int friend, String mText) {
        super(u, friend, mText);
    }

    public boolean setSubKey(int key) {
        subKey = key;
        return true;
    }

    public boolean setSubKey() {
        //sets the key to the key stored to a friend if no key is supplied
        //fails if the message does not have a user and friendID set
        //or friend has no set subkey
        if (super.getUser() != null && super.getFriendID() != 0) {
            Friend tFriend = super.getUser().findFriend(super.getFriendID());
            if (tFriend.getSubKey() == 0) {
                return false;
            }
            subKey = tFriend.getSubKey();
            return true;
        }
        return false;
    }

    //returns true if the message was encrypted successfully
    //false if message or key are not set/empty
    //encrypts the text of the message
    public boolean encrypt() {
        if (subKey == 0 || super.getText().equals("")) {
            return false;
        }
        String temp = super.getText().toUpperCase();
        String result = "";
        //shift all letters in the text to the right by key plus index
        for (int i = 0; i < temp.length(); i++) {
            char c = temp.charAt(i);
            if (c >= 65 && c <= 90) //check if each character is a letter
            {
                //shift char over by key plus index
                int shift = (int) c;
                shift -= 65;
                shift = shift + subKey + i;
                shift %= 26;
                shift += 65;
                c = (char) shift;
            }
            result += c;
        }
        super.setText(result);
        return true;
    }

    //returns true if the message was decrypted successfully
    //false if message or key are not set/empty
    //decrypts the text of the message
    public boolean decrypt() {
        if (subKey == 0 || super.getText().equals("")) {
            return false;
        }
        String temp = super.getText().toUpperCase();
        String result = "";
        //shift all letters in the text to the left by key plus index
        for (int i = 0; i < temp.length(); i++) {
            char c = temp.charAt(i);
            if (c >= 65 && c <= 90) //check if each character is a letter
            {
                //shift char over by key and index
                int shift = (int) c;
                shift -= 65;
                shift = shift - subKey - i;
                int modbase = 26;
                while (shift < 0) {
                    shift += modbase;
                }
                shift %= 26;
                shift += 65;
                c = (char) shift;
            }
            result += c;
        }
        super.setText(result);
        return true;
    }

    public static int generateKey() {                                            
        int key = 0;
        Random rand = new Random();
        do {
            key = rand.nextInt(999);
        } while (key % 26 == 0);
        return key;
    }
}